<template>
  <view class="goods-item" @click="goodsClickHandler">
    <!-- 商品左侧图片区域 -->
    <view class="goods-item-left">
      <radio :checked="goods.goods_state" color="#c00000" v-if="showRadio" @click="radioClickHandler"></radio>
      <image class="goods-image" :src="goods.goods_small_logo || defaultPic"></image>
    </view>
    <!-- 商品右侧信息区域 -->
    <view class="goods-item-right">
      <!-- 商品标题 -->
      <view class="goods-title">
        {{goods.goods_name}}
      </view>
      <!-- 商品价格 -->
      <view class="goods-info">
        <view class="goods-price">
          ¥{{goods.goods_price | tofixed}}
        </view>
        <uni-number-box :min="1" :value="goods.goods_count" v-if="showNumber" @change="numChangeHandler"></uni-number-box>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name:"my-goods",
    data() {
      return {
        // 后台数据返回无图片适配，默认的空图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    props: {
      // 是否显示选中按钮
      showRadio: {
        type: Boolean,
        default: false
      },
      // 是否显示商品数量
      showNumber: {
        type: Boolean,
        default: false
      },
      // 商品详情（一件）
      goods: {
        type: Object,
        default: {}
      }
    },
    filters: {
      // 生命过滤器函数
      tofixed(num){
        return Number(num).toFixed(2)
      }
    },

    methods: {
      // 商品选中点击事件处理函数
      radioClickHandler(){
        // 通知到父组件
        this.$emit('raido-change', {
          goods_id: this.goods.goods_id,
          goods_state: !this.goods.goods_state
        })
      },
      // 商品数量变化事件处理函数
      numChangeHandler(value){
        console.log('my-goods num change', value)
        // 调用父组件传入的函数啊
        this.$emit('num-change', {
          goods_id: this.goods.goods_id,
          goods_count: value
        })
      },
      goodsClickHandler(){
        this.$emit('click', this.goods)
      }
    }
    
  }
</script>

<style lang="scss">
    .goods-item {
      display: flex;
      border-bottom: 1px solid #efefef;
      padding: 8rpx;
      .goods-item-left {
        display: flex;
        align-items: center;
        justify-content: center;
        .goods-image {
          width: 200rpx;
          height: 200rpx;
          vertical-align: middle;
        }
      }
      .goods-item-right {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .goods-title {
          font-size: 24rpx;
        }
        .goods-info {
          display: flex;
          justify-content: space-between;
          align-items: center;
          .goods-price{
            font-size: 32rpx;
            color: #c00000;
          }
        }

      }
    }
</style>
